import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();

const app = getApp()
Page({

  data: {
    imgUrl: '', // app.globalData.uploadsUrl,
    devTypeList: [],
    devIndex: 0,
    cententDetail: {
      title: '',
      top: 0
    },
    infoList: [{
        name: '图片',
        infoindex: 0,
        id: 1
      },
      {
        name: '视频',
        infoindex: 1,
        id: 2
      }
    ],
    infoindex: 0,
    isEdit: false,
    isDelImg: false,
    isDelVideo: false,
    orgType: ''
  },

  //获取设备类型
  getDeviceTypeVO() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/device/queryTypeList',
      method: 'POST',
      data: {
        pageNo: 100,
      },
      loading: false
    }, (res) => {
      var list = res.list
      if (this.data.id) {
        list.map((item, idx) => {
          if (item.id == this.data.cententDetail.typeId) {
            this.setData({
              devIndex: idx
            })
          }
        })
      }

      console.log(this.data.devIndex, '-----------------')

      this.setData({
        devTypeList: list
      })
    })
  },


  onLoad: function (options) {
    this.uploadImgs = this.selectComponent("#uploadImgs");
    this.contentVideo = this.selectComponent("#contentVideo")

    let introItem = wx.getStorageSync('introItem')
    if (wx.getStorageSync('introItem')) {
      this.data.isEdit = true
      this.data.infoindex = Number(introItem.aboutType) - 1
      this.data.cententDetail = introItem
      let frontImg = this.data.imgUrl + this.data.cententDetail.imgPath
      console.log(this.data.cententDetail, frontImg);

      this.setData({
        isEdit: this.data.isEdit,
        infoindex: this.data.infoindex,
        cententDetail: this.data.cententDetail,
        frontImg,
      })
    }
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
    this.getDeviceTypeVO();
   
  },

  onShow: function () {
    this.setData({
      orgType: wx.getStorageSync('userInfo').orgType
    })
  },

  onUnload: function () {
    wx.removeStorageSync('introItem')
  },

  onHide: function () {
    wx.removeStorageSync('introItem')
  },

  //介绍视频回调
  contentVideoCallback: function () {
    this.setData({
      contentVideo: this.contentVideo.data.video,
      contentVideoTime: '00:' + this.contentVideo.data.duration,
    })
    console.log(this.data.contentVideo);
  },

  //选择商品图片
  chooseGoodsImg: function (e) {
    this.setData({
      imgs: this.uploadImgs.data.imgs,
    })
  },

  deleteGoodsImg: function (e) {
    this.setData({
      imgs: this.uploadImgs.data.imgs
    })
  },

  deloriginGoodsImg: function () {
    this.setData({
      originImgs: this.uploadImgs.data.originImgs
    })
  },

  deleteImg: function (e) {
    this.data.cententDetail.imgPath = ''
    this.data.frontImg = ''
    this.setData({
      isDelImg: true,
      frontImg: ''
    })
  },

  imgView: function (e) {
    var src = e.target.dataset.url; //获取data-src
    var imgList = [e.target.dataset.url]; //获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },

  //选择类型
  infoPickerChange: function (e) {
    var infoindex = e.detail.value;
    var infoList = this.data.infoList
    var typeId = '';

    infoList.map((item, index) => {
      if (index == infoindex) {
        typeId = item.id
      }
    })
    this.setData({
      infoindex,
      typeId
    })
  },

  //当编辑器内容发生改变获取编辑器内容
  onbindblur(e) {
    console.log(e, '编辑器')
    this.setData({
      editorContent: e.detail.html
    })
  },

  onCheckedChange(event) {
    this.setData({
      checked: event.detail
    });
  },

  //选择设备类型
  devPickerChange: function (e) {
    var val = e.detail.value
    this.setData({
      devIndex: val
    })

  },

  formSubmit: function (e) {
    var formdata = e.detail.value;
    var type = e.detail.target.dataset.type
    formdata.top = formdata.top ? 1 : 0
    formdata.typeId = this.data.devTypeList[this.data.devIndex].id
    formdata.aboutType = Number(this.data.infoindex) + 1
    formdata.content = this.data.editorContent

    var imgArr = this.uploadImgs.data.datalist

    if (imgArr && imgArr.length > 0 && Number(this.data.infoindex) == 0) {
      formdata.imgPath = imgArr[0].imgPath
      formdata.imgName = imgArr[0].imgName
    }

    if (Number(this.data.infoindex) == 1) {
      formdata.imgPath = this.data.contentVideo
    }

    if (!formdata.typeId) {
      wx.showToast({
        icon: 'none',
        title: '请选择说明内容',
      })
      return
    }

    if (!formdata.title) {
      wx.showToast({
        icon: 'none',
        title: '标题不能为空',
      })
      return
    }

    // if (!formdata.content) {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '内容详情不能为空',
    //   })
    //   return
    // }
      this.formRequest(formdata)

  },

  //请求
  formRequest: function (formdata) {
    var url = '/gds/protected/comm/beanCrud/iot.DeviceAboutVO/create';
    var title = "发布成功"

    //修改
    if (this.data.cententDetail && this.data.cententDetail.id) {
      url = "/gds/protected/comm/beanCrud/iot.DeviceAboutVO/update";
      formdata.id = this.data.cententDetail.id;
      title = "修改成功"
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: formdata
    }, (res) => {
      console.log(res)
      wx.showToast({
        title,
      })
      setTimeout(() => {
        wx.redirectTo({
          url: "/pages/about_device/list",
        })
      }, 1500)
    })
  },


})